home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / edit / thesrc20.zip / manext.c < prev    next >
C/C++ Source or Header  |  1995-01-26  |  10KB  |  313 lines

  1. /***********************************************************************/
  2. /* MANEXT - Extract manual pages from C source code.                   */
  3. /***********************************************************************/
  4. /*
  5.  * MANEXT - A program to extract manual pages from C source code.
  6.  * Copyright (C) 1991-1995 Mark Hessling
  7.  *
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License as
  10.  * published by the Free Software Foundation; either version 2 of
  11.  * the License, or any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16.  * General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to:
  20.  *
  21.  *    The Free Software Foundation, Inc.
  22.  *    675 Mass Ave,
  23.  *    Cambridge, MA 02139 USA.
  24.  *
  25.  *
  26.  * If you make modifications to this software that you feel increases
  27.  * it usefulness for the rest of the community, please email the
  28.  * changes, enhancements, bug fixes as well as any and all ideas to me.
  29.  * This software is going to be maintained and enhanced as deemed
  30.  * necessary by the community.
  31.  *
  32.  * Mark Hessling                     email: M.Hessling@gu.edu.au
  33.  * 36 David Road                     Phone: +61 7 849 7731
  34.  * Holland Park                      Fax:   +61 7 875 5314
  35.  * QLD 4121
  36.  * Australia
  37.  */
  38.  
  39. /*
  40. $Id: manext.c 2.0 1995/01/26 16:31:23 MH Release MH $
  41. */
  42.  
  43. #include <stdio.h>
  44.  
  45. #ifdef PROTO
  46. void display_info(void);
  47. #else
  48. void display_info();
  49. #endif
  50.  
  51. typedef unsigned char bool;
  52. typedef char CHARTYPE;
  53.  
  54. #define TRUE    1
  55. #define FALSE   0
  56.  
  57. #define MAX_LINE 255
  58.  
  59. #define FORMAT_MANUAL     0
  60. #define FORMAT_QUICK_REF  1
  61.  
  62. #define STATE_IGNORE    0
  63. #define STATE_SYNTAX    1
  64. #define STATE_COMMAND   2
  65. /***********************************************************************/
  66. #ifdef PROTO
  67. short strzne(CHARTYPE *str,CHARTYPE ch)
  68. #else
  69. short strzne(str,ch)
  70. CHARTYPE *str;
  71. CHARTYPE ch;
  72. #endif
  73. /***********************************************************************/
  74. {
  75. /*--------------------------- local data ------------------------------*/
  76.  register short len=0;
  77.  register short  i = 0;
  78. /*--------------------------- processing ------------------------------*/
  79.  len = strlen(str);
  80.  for (; i<len && str[i]==ch; i++);
  81.  if (i>=len)
  82.     i = (-1);
  83.  return(i);
  84. }
  85. /***********************************************************************/
  86. #ifdef PROTO
  87. short strzrevne(CHARTYPE *str,CHARTYPE ch)
  88. #else
  89. short strzrevne(str,ch)
  90. CHARTYPE *str;
  91. CHARTYPE ch;
  92. #endif
  93. /***********************************************************************/
  94. {
  95. /*--------------------------- local data ------------------------------*/
  96.  register short len=0;
  97. /*--------------------------- processing ------------------------------*/
  98.  len = strlen(str);
  99.  for (--len; len>=0 && str[len]==ch; len--);
  100.  return(len);
  101. }
  102. /***********************************************************************/
  103. #ifdef PROTO
  104. bool blank_field(CHARTYPE *field)
  105. #else
  106. bool blank_field(field)
  107. CHARTYPE *field;
  108. #endif
  109. /***********************************************************************/
  110. {
  111. /*--------------------------- local data ------------------------------*/
  112. /*--------------------------- processing ------------------------------*/
  113.  if (strzne(field,' ') == (-1))
  114.     return(TRUE);                /* field is NULL or just contains spaces */
  115.  return(FALSE);
  116. }
  117. /***********************************************************************/
  118. #ifdef PROTO
  119. CHARTYPE *strtrunc(CHARTYPE *string)
  120. #else
  121. CHARTYPE *strtrunc(string)
  122. CHARTYPE *string;
  123. #endif
  124. /***********************************************************************/
  125. {
  126. /*--------------------------- local data ------------------------------*/
  127.  register short i=0;
  128.  short pos=0;
  129. /*--------------------------- processing ------------------------------*/
  130.  pos = strzrevne(string,' ');
  131.  if (pos == (-1))
  132.     strcpy(string,"");
  133.  else
  134.     *(string+pos+1) = '\0';
  135.  pos = strzne(string,' ');
  136.  if (pos != (-1))
  137.    {
  138.     for (i=0;*(string+i)!='\0';i++)
  139.        *(string+i) = *(string+i+pos);
  140.     *(string+i) = '\0';
  141.    }
  142.  return(string);
  143. }
  144. /***********************************************************************/
  145. #ifdef PROTO
  146. CHARTYPE *strtrim(CHARTYPE *string,char ch)
  147. #else
  148. CHARTYPE *strtrim(string,ch)
  149. CHARTYPE *string,ch;
  150. #endif
  151. /***********************************************************************/
  152. {
  153. /*--------------------------- local data ------------------------------*/
  154.  register short i=0;
  155.  short pos=0;
  156. /*--------------------------- processing ------------------------------*/
  157.  for (i=0;i<strlen(string);i++)
  158.    {
  159.     if (*(string+i) == ch)
  160.        return(string+i+1);
  161.    }
  162.  return(string);
  163. }
  164. /***********************************************************************/
  165. #ifdef PROTO
  166. int main(int argc,char *argv[])
  167. #else
  168. int main(argc,argv)
  169. int argc;
  170. char *argv[];
  171. #endif
  172. /***********************************************************************/
  173. {
  174.  char    s[MAX_LINE + 1];        /* input line */
  175.  char    save_line[MAX_LINE + 1];
  176.  register int     i = 0;
  177.  FILE *fp;
  178.  char c;
  179.  char append=0;
  180.  int format=FORMAT_MANUAL;
  181.  int state=STATE_IGNORE;
  182.  int file_start=1;
  183.  
  184. #ifdef __EMX__
  185.  _wildcard(&argc,&argv);
  186. #endif
  187.  
  188.  if (strcmp(argv[1],"-h") == 0)
  189.    {
  190.     display_info();
  191.     exit(1);
  192.    }
  193.  if (strcmp(argv[1],"-q") == 0) /* generate quick reference */
  194.    {
  195.     format = FORMAT_QUICK_REF;
  196.     file_start = 2;
  197.    }
  198.  for(i=file_start;i<argc;i++)
  199.     {
  200.      if ((fp = fopen(argv[i],"r")) == NULL)
  201.        {
  202.         fprintf(stderr,"\nCould not open %s\n",argv[i]);
  203.         continue;
  204.        }
  205.      while(1)
  206.        {
  207.         if (fgets(s, (int)sizeof(s), fp) == NULL)
  208.           {
  209.            if (ferror(fp) != 0)
  210.              {
  211.               fprintf(stderr, "*** Error reading %s.  Exiting.\n",argv[i]);
  212.               exit(1);
  213.              }
  214.            break;
  215.           }
  216.  
  217.         /* check for manual entry marker at beginning of line */
  218.         if (strncmp(s, "/*man-start*", 12) != 0)
  219.             continue;
  220.  
  221.         state = STATE_IGNORE;
  222.         /* inner loop */
  223.         for (;;)
  224.            {
  225.             /* read next line of manual entry */
  226.             if (fgets(s, (int)sizeof(s), fp) == NULL)
  227.               {
  228.                if (ferror(fp) != 0)
  229.                  {
  230.                   fprintf(stderr, "*** Error reading %s.  Exiting.\n",argv[i]);
  231.                   exit(1);
  232.                  }
  233.                 break;
  234.               }
  235.             /* check for end of entry marker */
  236.             if (strncmp(s, "**man-end", 9) == 0)
  237.                break;
  238.             switch(format)
  239.               {
  240.                case FORMAT_MANUAL:
  241.                     printf("     %s",s);
  242.                     break;
  243.                case FORMAT_QUICK_REF:
  244.                     s[strlen(s)-1] = '\0';
  245.                     switch(state)
  246.                       {
  247.                        case STATE_IGNORE:
  248.                             if (strncmp(s, "COMMAND", 7) == 0)
  249.                               {
  250.                                state = STATE_COMMAND;
  251.                                break;
  252.                               }
  253.                             if (strncmp(s, "SYNTAX", 6) == 0)
  254.                               {
  255.                                state = STATE_SYNTAX;
  256.                                break;
  257.                               }
  258.                             break;
  259.                        case STATE_COMMAND:
  260.                             strcpy(save_line,s);
  261.                             state = STATE_IGNORE;
  262.                             break;
  263.                        case STATE_SYNTAX:
  264.                             if (blank_field(s))
  265.                               {
  266.                                printf("\t%s\n",strtrunc(strtrim(save_line,'-')));
  267.                                state = STATE_IGNORE;
  268.                                break;
  269.                               }
  270.                                printf(" %s\n",strtrunc(s));
  271.                             break;
  272.                        default:
  273.                             break;
  274.                       }
  275.                     break;
  276.                default:
  277.                     break;
  278.               }
  279.             }
  280.         if (format == FORMAT_MANUAL)
  281.            printf("\n\n\n     --------------------------------------------------------------------------\n");
  282.  
  283.         /* check if end of file */
  284.         if (feof(fp) != 0)
  285.             break;
  286.        }
  287.      fclose(fp);
  288.     }
  289.  if (format == FORMAT_MANUAL)
  290.     printf("\n\n\n\n\n");
  291.  return(0);
  292. }
  293. /***********************************************************************/
  294. #ifdef PROTO
  295. void display_info(void)
  296. #else
  297. void display_info()
  298. #endif
  299. /***********************************************************************/
  300. {
  301. /*--------------------------- local data ------------------------------*/
  302. /*--------------------------- processing ------------------------------*/
  303.  
  304.  fprintf(stderr,"\nMANEXT 1.00 Copyright (C) 1991-1995 Mark Hessling\n");
  305.  fprintf(stderr,"All rights reserved.\n");
  306.  fprintf(stderr,"MANEXT is distributed under the terms of the GNU\n");
  307.  fprintf(stderr,"General Public License and comes with NO WARRANTY.\n");
  308.  fprintf(stderr,"See the file COPYING for details.\n");
  309.  fprintf(stderr,"\nUsage: MANEXT sourcefile [...]\n\n");
  310.  fflush(stderr);
  311.  return;
  312. }
  313.